package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.membershipcard;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.adapter.FuCardListAdapter;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.bean.GetMainCardBean;
import com.zzsy.carosshopprojects.bean.MessageEvent;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.ConverterHelper;
import com.zzsy.carosshopprojects.util.DataUtil;
import com.zzsy.carosshopprojects.util.StringUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.math.BigDecimal;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * 卡片列表
 */
public class FuCardListFragment extends BaseFragment {
    public static FuCardListFragment newInstance(String uuid, String productId) {
        FuCardListFragment fuCardListFragment = new FuCardListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("uuid", uuid);
        bundle.putString("productId", productId);
        fuCardListFragment.setArguments(bundle);
        return fuCardListFragment;
    }

    @BindView(R.id.rv_fa)
    RecyclerView recycleView;
    @BindView(R.id.tv_add_card)
    TextView tvAddCard;
    private FuCardListAdapter fuCardListAdapter;
    private String mainCardId;
    private String productId;

    @Override
    protected int initLayout() {
        return R.layout.fg_fucardlist;
    }

    @Override
    protected void initView(View view) {
        initTitle(view, getResources().getString(R.string.CardList), getActivity());
        fuCardListAdapter = new FuCardListAdapter(getContext(), null, 0);
        recycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        recycleView.setAdapter(fuCardListAdapter);
        fuCardListAdapter.setOnItemClickListener((v, position) -> {
            String cardId = fuCardListAdapter.getListData().get(position).getId();
            String payLimit = fuCardListAdapter.getListData().get(position).getPayLimit();
            String phone = fuCardListAdapter.getListData().get(position).getPhone();
            String realName = fuCardListAdapter.getListData().get(position).getRealName();
            String currentStatus = fuCardListAdapter.getListData().get(position).getCurrentStatus();
            String cardType = fuCardListAdapter.getListData().get(position).getSubCardType();
            String payUseLimit=ConverterHelper.toBigDecimal(fuCardListAdapter.getListData().get(position).getUseLimit()).divide(ConverterHelper.toBigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
            if (!StringUtils.isEmpty(currentStatus)) {
                if ("2".equals(currentStatus)) {
                    start(BuCardFragment.newInstance(cardId, payLimit, phone, realName));
                } else {
                    if (!StringUtils.isEmpty(cardId)) {
                        start(ManageCardFragment.newInstance(cardId, payLimit,payUseLimit, phone, realName,cardType));
                    }
                }
            }
        });
        tvAddCard.setOnClickListener(view1 -> {
            if (!StringUtils.isEmpty(mainCardId)) {
                start(AddCardFragment.newInstance(mainCardId, productId));
            } else {
                ToastUtils.showToast(getContext().getApplicationContext(), "主卡信息异常！");
            }
        });
    }

    @Override
    protected void initData(Context mContext) {
        productId = getArguments().getString("productId");
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void loadMethod() {
        getDataList();
    }

    private void getDataList() {
        RequestUtils.MemberSubCardsByUuid(getContext(), DataUtil.getInstance().getCompanyId(), getArguments().getString("uuid"), new MyObserver<GetMainCardBean>(getContext()) {
            @Override
            public void onSuccess(GetMainCardBean result) {
                mainCardId = result.getMainCardId();
                boolean cardSwitch = result.isCardSwitch();
                if (!cardSwitch) {
                    tvAddCard.setVisibility(View.GONE);
                } else {
                    tvAddCard.setVisibility(View.VISIBLE);
                }
                fuCardListAdapter.setListData(result.getMyGrantSubCards());
                fuCardListAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(getContext().getApplicationContext(), errorMsg);
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        switch (event.message) {
            case MessageEvent.CARD:
                getDataList();
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
